﻿function doajax(url, d, f, e) {
    var o = {};
    var cd = [];
    var mdata = {};
    var jsonData = "";
    //查询操作
    if (d[0].opt == "select") {
        $.each(d, function (i, item) {
            if (i != 0) {
                o["Name"] = item.item.Name;
                o["Operation"] = item.item.Operation;
                o["Value"] = item.item.Value;
                o["ValueType"] = item.item.ValueType;
                cd.push(JSON.stringify(o));
            }
        })
        jsonData = "[" + cd.join(",") + "]";
    }
        //新增或修改操作
    else {
        //updatedelcache更新后删除缓存的情况
        if (d[0].Id != "" && d[0].Id != "0" && (d[0].opt == "update" || d[0].opt == "updatedelcache")) {
            mdata["Id"] = d[0].Id;
        }
        jsonData = JSON.stringify(d[1].item);
    }


    mdata["JsonData"] = jsonData;
    mdata["CollectionName"] = d[0].name;
    $.ajax({
        type: "POST",
        data: mdata,
        dataType: "text",
        url: url,
        async: false,
        success: function (b) {
            f(b);
        },
        error: function (b) {
            if (e) {
                e(b);
                $("#loading").removeClass("loading");
                $("#loading").addClass("hide");
            }
        }
    })
}

function doajaxasync(url, d, ayc, f, e) {
    var o = {};
    var cd = [];
    var mdata = {};
    var jsonData = "";
    //查询操作
    if (d[0].opt == "select") {
        $.each(d, function (i, item) {
            if (i != 0) {
                o["Name"] = item.item.Name;
                o["Operation"] = item.item.Operation;
                o["Value"] = item.item.Value;
                o["ValueType"] = item.item.ValueType;
                cd.push(JSON.stringify(o));
            }
        })
        jsonData = "[" + cd.join(",") + "]";
    }
        //新增或修改操作
    else {

        if (d[0].Id != "" && d[0].Id != "0" && d[0].opt == "update") {
            mdata["Id"] = d[0].Id;
        }
        jsonData = JSON.stringify(d[1].item);
    }


    mdata["JsonData"] = jsonData;
    mdata["CollectionName"] = d[0].name;
    $.ajax({
        type: "POST",
        data: mdata,
        dataType: "text",
        url: url,
        async: ayc,
        success: function (b) {
            f(b);
        },
        error: function (b) {
            if (e) {
                e(b);
                $("#loading").removeClass("loading");
                $("#loading").addClass("hide");
            }
        }
    })
}

function doajax1async(url, data, ayc, f) {
    var mdata = data;
    $.ajax({
        type: "POST",
        data: mdata,
        dataType: "json",
        url: url,
        async: ayc,
        success: function (b) {
            f(b);
        }
    })

}

/**
* 获得字符串实际长度，中文2，英文1
* @param str：要获得长度的字符串
*/
var GetLength = function (str) {
    var realLength = 0, len = str.length, charCode = -1;
    for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128) realLength += 1;
        else realLength += 2;
    }
    return realLength;
};

/** 
* js截取字符串，中英文都能用 
* @param str：需要截取的字符串 
* @param len: 需要截取的长度 
*/
function cutstr(str, len) {
    var str_length = 0;
    var str_len = 0;
    str_cut = new String();
    str_len = str.length;
    for (var i = 0; i < str_len; i++) {
        a = str.charAt(i);
        str_length++;
        if (escape(a).length > 4) {
            //中文字符的长度经编码之后大于4  
            str_length++;
        }
        str_cut = str_cut.concat(a);
        if (str_length >= len) {
            str_cut = str_cut.concat("...");
            return str_cut;
        }
    }
    //如果给定字符串小于指定长度，则返回源字符串；  
    if (str_length < len) {
        return str;
    }
}

